package com.ncp.model.biz.registerlogin;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.servlet.jsp.jstl.sql.Result;

import com.ncp.model.dao.DB;
import com.ncp.model.dao.DbConnection;
import com.ncp.model.entity.registerlogin.UserBasicBean;
import common.util.security.EncryptUtil;

public class RegisterBiz {
	private  DB common = new DB();
	private PreparedStatement pstmt=null;
	private Connection conn=null;
	private ResultSet rs=null;
	/**
	 * 
	 * @param UserBasic
	 * @return
	 * @throws SQLException 
	 */
	public boolean insertregisterUser(UserBasicBean UserBasic) throws SQLException{
		boolean b=false;
		String userLoginName = UserBasic.getUserLoginName();
		String userPassword = UserBasic.getPassWord();
		EncryptUtil encrypt = new EncryptUtil();
		String passWordSHA = encrypt.sha(userPassword);
		String userName = UserBasic.getUserName();
		String userEmail = UserBasic.getEmail();
		String userTel = UserBasic.getTel();
		String userAddress = UserBasic.getAddress();
		int levelId =  UserBasic.getLevelID();      
		String status = UserBasic.getStatus();
		String ActiveCode=UserBasic.getActiveCode();
		String ActiveCodeSHA=encrypt.sha(ActiveCode);
	    String sql="insert into USERBASIC " +
		"(UserLoginName,UserImage,UserImageBef,PassWord,LevelID,UserName,EMail,Tel,Address,RegisterDate,Status,ActiveCode) " +
				"values('"+userLoginName+"','images/UserImage/ncp.png','images/UserImage/ncp.png','"+passWordSHA+"','"+ levelId+"','"+userName+"','"+userEmail+"','"+userTel+"','"+userAddress+"',now(),'"+status+"','"+ActiveCodeSHA+"')" ;
			common.setSql(sql);
			common.setConn(DbConnection.getConnByDataSource());
			List<Object> list=new ArrayList<Object>();
			common.setList(list);
			int r=common.executeUpdate();
			if(r!=0){
				b=true;}
		return b;
	}
	public boolean isActived(String ActiveCodeSHA){
		/**
		 * 查找对应激活码的用户是否已经激活
		 * -1代表对应用户已经激活。
		 * 1代表对应用户没有激活
		 */
		boolean b=false;
		String sql = "select Status from USERBASIC where ActiveCode='"+ActiveCodeSHA+"'";
		try {
			conn=DbConnection.getConnByDataSource();
			pstmt=conn.prepareStatement(sql);
			rs=pstmt.executeQuery();
			if(rs.next()){
				if(rs.getString(1).equals("true")){
					return b=true;
				}
			}
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}finally{
			common.closeSQL(conn, pstmt);
		}
		return b;
	}
	/**
	 * 这个方法实现激活用户账号
	 * @param ActiveCodeSHA
	 * @return
	 */
	public boolean UserActive(String ActiveCodeSHA){
		boolean b=false;
		String sql="update USERBASIC set Status='true' where ActiveCode='"+ActiveCodeSHA+"'";
		common.setSql(sql);
		common.setConn(DbConnection.getConnByDataSource());
		List<Object> list=new ArrayList<Object>();
		common.setList(list);
		int r=common.executeUpdate();
		if(r!=0){
			b=true;
		}
		return b;
	}
	public boolean ResetPassword(UserBasicBean UserBasic,String ActiveCode){
		boolean b=false;
		String userPassword = UserBasic.getPassWord();
		EncryptUtil encrypt = new EncryptUtil();
		String passWordSHA = encrypt.sha(userPassword);
		String sql="update USERBASIC set PassWord='"+passWordSHA+"' where ActiveCode=?";
		common.setSql(sql);
		common.setConn(DbConnection.getConnByDataSource());
		List<Object> list=new ArrayList<Object>();
		common.setList(list);
		int r=common.executeUpdate();
		if(r!=0){
			b=true;
		}
		return b;
	}
	
	/***获取修改注册信息页面需要显示的用户信息***/
	public List<UserBasicBean> getList(String userLoginName){
		
		String sql="select UserID,UserLoginName,UserImage,PassWord,UserName,EMail,Tel,Address from USERBASIC where UserLoginName= ? ";
		common.setSql(sql);
		common.setConn(DbConnection.getConnByDataSource());
		List<Object> list=new ArrayList<Object>();
		list.add(0,userLoginName);
		common.setList(list);
		Result rs = common.executeQuery();
		/***获取数据***/
		List<UserBasicBean> list2 = new ArrayList<UserBasicBean>();
		/***这里虽然采用for循环，但是实际上，只有一条记录,之所以写成这种方式，是采用的通常写法***/
		for(Map<String,Object> object: rs.getRows()){
			UserBasicBean UserBasic = new UserBasicBean();
			UserBasic.setUserID(Integer.parseInt(object.get("UserID").toString()));
			UserBasic.setUserLoginName(object.get("UserLoginName").toString());
			UserBasic.setPassWord(object.get("PassWord").toString());
			UserBasic.setUserName(object.get("UserName").toString());
			UserBasic.setEmail(object.get("EMail").toString());
			UserBasic.setTel(object.get("Tel").toString());
			UserBasic.setAddress(object.get("Address").toString());
			UserBasic.setUserImg(object.get("UserImage").toString());
			list2.add(UserBasic);
		}
		return list2;
	}
	/***修改注册信息***/
	public int modifyRegisterUserInfo(String userLoginName,UserBasicBean UserBasic){
		String userName = UserBasic.getUserName();
		String userEmail = UserBasic.getEmail();
		String userTel = UserBasic.getTel();
		String userAddress = UserBasic.getAddress();
		String sql="update USERBASIC set UserName='"+userName+"',EMail='"+userEmail+"',Tel='"+userTel+"',Address='"+userAddress+"' where UserLoginName='"+userLoginName+"'";
		common.setSql(sql);
		common.setConn(DbConnection.getConnByDataSource());
		List<Object> list=new ArrayList<Object>();
		//list.add(0,userLoginName);
		common.setList(list);
		return common.executeUpdate();
	}
	public int modifypwd(UserBasicBean UserBasic){
		String userpwd = UserBasic.getPassWord();
		EncryptUtil encrypt = new EncryptUtil();
		String passWordSHA = encrypt.sha(userpwd);
		String userLoginName = UserBasic.getUserLoginName();
		String sql="update USERBASIC set PassWord='"+passWordSHA+"' where UserLoginName='"+userLoginName+"'";
		common.setSql(sql);
		common.setConn(DbConnection.getConnByDataSource());
		List<Object> list=new ArrayList<Object>();
		//list.add(0,userLoginName);
		common.setList(list);
		return common.executeUpdate();	
	}
	public int resetpwd(String email,UserBasicBean UserBasic){
		String userpwd = UserBasic.getPassWord();
		EncryptUtil encrypt = new EncryptUtil();
		String passWordSHA = encrypt.sha(userpwd);
		String sql="update USERBASIC set PassWord='"+passWordSHA+"' where EMail='"+email+"'";
		common.setSql(sql);
		common.setConn(DbConnection.getConnByDataSource());
		List<Object> list=new ArrayList<Object>();
		//list.add(0,userLoginName);
		common.setList(list);
		return common.executeUpdate();	
	}
}


